package com.mission.shaco.warehouse.models;

import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import java.util.Iterator;

/**
 * Created by Administrator on 2017/7/14.
 */

public class Grids implements Serializable {

    public static final String  GRID_KEY = "grids_number";
    public static JSONObject gridJson = new JSONObject();
    public static int gridNumber = 20;

    public Grids() throws JSONException {
        readGrids();

    }

    public static JSONObject readGrids() throws JSONException {
        gridJson =  ClientConfig.getJSONObject(GRID_KEY);
        return gridJson;
    }

    public static void saveGrids() {
        ClientConfig.setJSONObject(GRID_KEY,gridJson);
    }

    public static void setGridNumber(int number){
        gridNumber = number;
    }

    public static void calculationTotal(){

    }

    public static void clear(){
        gridJson = new JSONObject();
        saveGrids();
    }

    public static Boolean isGridFullSet() throws JSONException {
        Iterator it = gridJson.keys();
        int i=0;
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = gridJson.getString(key);
            if(TextUtils.isEmpty(value)){
                return false;
            }
            i++;
        }
        if(i < gridNumber){
            return false;
        }

        return true;
    }


    public static Boolean hasValue() throws JSONException {

        if(gridJson.length() == 0 ){
            return false;
        }
        Iterator iterator = gridJson.keys();
        while(iterator.hasNext()){
            String key = (String) iterator.next();
            if(TextUtils.isEmpty(gridJson.getString(key))){
                return false;
            }
        }
        return true;
    }
}
